package com.restaurant.web;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.restaurant.domain.Menu;
import com.restaurant.domain.Sidedishmenu;
import com.restaurant.domain.SidedishmenuPK;
import com.restaurant.proxies.SideDeshMenuProxy;
import com.restaurant.service.MenuService;
import com.restaurant.service.SidedishmenuService;

@Component
@RequestMapping("/action/sidedeshmenu.do")
@SessionAttributes("sideDeshMenuProxy")
public class MenuSideDishFormController {
	
	private final String queryLoadAllMenu = "from Menu where isdeleted = false";
	
	@Autowired
	private SidedishmenuService objSidedishmenuService;
	
	@Autowired
	private MenuService objMenuService;
	
	@RequestMapping(method=RequestMethod.GET)
	public String menuSideDishGETRequest(ModelMap map){
		SideDeshMenuProxy sideDeshMenuProxy = new SideDeshMenuProxy();		
		try {
			sideDeshMenuProxy.setListAllMenu((ArrayList<Menu>)objMenuService.loadByHql(queryLoadAllMenu));
		} catch (Exception e) {
			e.printStackTrace();
		}
		map.addAttribute("sideDeshMenuProxy", sideDeshMenuProxy);
		return "SideDishMenu";
	}
	
	@RequestMapping(method=RequestMethod.POST)
	public String menuSideDishPOSTRequest(ModelMap map, @ModelAttribute SideDeshMenuProxy sideDeshMenuProxy, BindingResult result, HttpServletRequest request, HttpServletResponse response){
		String action = request.getParameter("action");
		
		if(action.compareTo("onselect")==0){
			if(sideDeshMenuProxy.getMenu().getMenuID()!=0)
				map = onSelectMenu(map, sideDeshMenuProxy, request, response);
			else
				return "redirect:/sidedeshmenu.do";
		}else if(action.compareTo("edit")==0){
			map = onAddSideDish(map, sideDeshMenuProxy, request, response);
		}else if(action.compareTo("del")==0){
			map = deleteSidedishmenu(map, sideDeshMenuProxy, request, response);
		}
		
		return "SideDishMenu";
	}
	
	private ModelMap onSelectMenu(ModelMap map, SideDeshMenuProxy sideDeshMenuProxy, HttpServletRequest request, HttpServletResponse response){
		String menuID = sideDeshMenuProxy.getMenu().getMenuID().toString();
		
		
		//Set the Menu list which are not selected menu side dishes
		String queryLoadNotSidedishmenu = "select distinct m from Menu as m";
		queryLoadNotSidedishmenu += " where m.isdeleted = false and m.menuID not in";
		queryLoadNotSidedishmenu += " (select distinct md.menu.menuID from Sidedishmenu as md where md.menu1.menuID = " + menuID ;
		queryLoadNotSidedishmenu += ") and m.menuID != " + menuID ;
		
		ArrayList<Menu> listMenu = null;
		try {
			listMenu = (ArrayList<Menu>) objMenuService.loadByHql(queryLoadNotSidedishmenu);
		} catch (Exception e) {
			e.printStackTrace();
		}
		sideDeshMenuProxy.setListMenu(listMenu);
		
		
		String queryLoadMenuSidedish = "select distinct m from Menu as m";
		queryLoadMenuSidedish += " where m.menuID in";
		queryLoadMenuSidedish += " (select distinct md.menu.menuID from Sidedishmenu as md where md.menu1.menuID = " + menuID ;
		queryLoadMenuSidedish += ") and m.menuID != " + menuID ;
		
		ArrayList<Menu> listMenuSidedish = null;
		try {
			listMenuSidedish = (ArrayList<Menu>) objMenuService.loadByHql(queryLoadMenuSidedish);
		} catch (Exception e) {
			e.printStackTrace();
		}
		sideDeshMenuProxy.setListSidedishmenus(listMenuSidedish);
		try {
			sideDeshMenuProxy.setMenu(objMenuService.loadByPrimaryKey(sideDeshMenuProxy.getMenu().getMenuID()));
		} catch (Exception e) {
			sideDeshMenuProxy.setMenu(new Menu());
		}
		
		try {
			sideDeshMenuProxy.setListAllMenu((ArrayList<Menu>)objMenuService.loadByHql(queryLoadAllMenu));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		map.addAttribute("sideDeshMenuProxy", sideDeshMenuProxy);
		return map;
	}
	
	
	private ModelMap onAddSideDish(ModelMap map, SideDeshMenuProxy sideDeshMenuProxy, HttpServletRequest request, HttpServletResponse response){
		StringBuffer ids = new StringBuffer(request.getParameter("ids"));
		ids.deleteCharAt(ids.lastIndexOf(","));
		ids.insert(0, "(");
		ids.insert(ids.length(), ")");
		
		StringBuffer query = new StringBuffer("from Menu where menuID in ");
		query.append(ids);
		
		ArrayList<Menu> listMenu = null;
		Menu menu = null;
		Sidedishmenu sidedishmenu = null;
		SidedishmenuPK sidedishmenuPK = null;
		try {
			listMenu = (ArrayList<Menu>) objMenuService.loadByHql(query.toString());
			menu = objMenuService.loadByPrimaryKey(sideDeshMenuProxy.getMenu().getMenuID());
			
			for(int i=0 ;i<listMenu.size();i++){
				
				sidedishmenuPK = new SidedishmenuPK();
				sidedishmenuPK.setMenuMenuID(menu.getMenuID());
				sidedishmenuPK.setMenuMenuIDSidedishID(listMenu.get(i).getMenuID());
				
				sidedishmenu = new Sidedishmenu();
				
				sidedishmenu.setMenu(listMenu.get(i));
				sidedishmenu.setMenu1(menu);
				sidedishmenu.setSidedishmenuPK(sidedishmenuPK);
				
				
				objSidedishmenuService.addOrModify(sidedishmenu);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		return onSelectMenu(map, sideDeshMenuProxy, request, response);
	}
	
	private ModelMap deleteSidedishmenu(ModelMap map, SideDeshMenuProxy sideDeshMenuProxy, HttpServletRequest request, HttpServletResponse response){
		
		StringBuffer ids = new StringBuffer(request.getParameter("ids"));
		ids.deleteCharAt(ids.lastIndexOf(","));
		ids.insert(0, "(");
		ids.insert(ids.length(), ")");
		
		StringBuffer query = new StringBuffer("from Sidedishmenu where menu.menuID in ");
		query.append(ids);
		
		ArrayList<Sidedishmenu> listSidedishmenus = null;
		try {
			listSidedishmenus = (ArrayList<Sidedishmenu>)objSidedishmenuService.loadByHql(query.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		for(int i = 0 ; i < listSidedishmenus.size(); i++){
			try {
				objSidedishmenuService.deleteRecord(listSidedishmenus.get(i));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return onSelectMenu(map, sideDeshMenuProxy, request, response);
	}
}
